package com.hystericalporpoises.thingamajava.repository;

import java.util.ArrayList;
import java.util.List;

import com.hystericalporpoises.thingamajava.DatabaseProvider;

/**
 * @author ian.g.simpson
 *
 * @param <T>
 */
public abstract class TestObjectRepository<T> {

  private DatabaseProvider databaseProvider;

  private List<T> instances = new ArrayList<T>();

  private T mostRecent;

  public void setDatabaseProvider(DatabaseProvider databaseProvider) {
    this.databaseProvider = databaseProvider;
  }

  public void manageInstance(T instance) {
    databaseProvider.save(instance);
    instances.add(instance);
    mostRecent = instance;
  }

  public abstract T getInstance();

  public void setMostRecent(T instance) {
    mostRecent = instance;
  }

  public T getMostRecent() {
    return mostRecent;
  }

  public void tearDown() {
    for (T instance : instances) {
      databaseProvider.delete(instance);
    }
    instances.clear();
  }

}
